#include "Cutter.h"
#include "GameMap.h"

#include <QDebug>

Cutter::Cutter(GridVec pos, int name, int direction)
    : Building(pos, name, direction)
{
    FirstRequire_ms = 2700;
    SecondRequire_ms = 1800;
}

std::vector<GridVec> Cutter::BuildingAllPos()
{
    std::vector<GridVec> allpos;
    allpos.push_back(pos);
    GridVec temps[] = {GridVec{pos.j + 1, pos.i},
                       GridVec{pos.j - 1, pos.i},
                       GridVec{pos.j, pos.i - 1},
                       GridVec{pos.j, pos.i + 1},
                      };
    allpos.push_back(temps[direction]);
    return allpos;
}

bool Cutter::IsPlace(const GridVec &click, int picdirection, GameMap &gamemap)
{
    for (auto pos : BuildingAllPos())
    {
        // 如果超出地图范围，返回false
        if (pos.i < 0 || pos.i >= GameInfo::HEIGHT || pos.j < 0 || pos.j >= GameInfo::WIDTH)
        {
            return false;
        }
        // 如果在矿地上，或有障碍物，返回false
        if (gamemap.GetResource(pos))
        {
            return false;
        }
        // 如果点击的是hub，返回false
        if (gamemap.GetBuilding(pos))
        {
            if (gamemap.GetBuilding(pos)->name == BuildingType::HUB || gamemap.GetBuilding(pos)->name == BuildingType::CUTTER || gamemap.GetBuilding(pos)->name == BuildingType::TRASH)
            {
                return false;
            }
        }
    }
    return true;
}

bool Cutter::CanReceive(GridVec target, int directionin, int shapename)
{
    if (state == TickableStatus::EMPTY)
    {
        if (directionin == direction)
        {
            if (shapename == Goods::CYCLE)
            {
                return true;
            }
        }
    }
    return false;
}

void Cutter::Receive(GridVec target, int directionin, int shapename)
{
    shape.name = shapename;
    state = TickableStatus::RUNNING;
    timer.Reset();
}

void Cutter::TickableRunning()
{
    timer.UpdateRuningTime(FirstRequire_ms);
    running_ms = timer.runningMs;
    return;
}

void Cutter::UpdateTickableState(GameMap &gamemap)
{

    switch (state)
    {
        case TickableStatus::EMPTY:
            running_ms = 0;
            break;
        case TickableStatus::RUNNING:
            if (running_ms >= FirstRequire_ms)
            {
                // cutter好了，准备运输
                state = TickableStatus::BLOCK;
                running_ms = 0;
            }
            else
            {
                TickableRunning();
            }
            break;
        case TickableStatus::BLOCK:
            switch (direction)
            {
                case UP:
                case RIGHT:
                    if (CanSend(BuildingAllPos()[0], direction, Goods::LEFT_CYCLE, gamemap) && CanSend(BuildingAllPos()[1], direction, Goods::RIGHT_CYCLE, gamemap))
                    {
                        Send(BuildingAllPos()[0], direction, Goods::LEFT_CYCLE, gamemap);
                        Send(BuildingAllPos()[1], direction, Goods::RIGHT_CYCLE, gamemap);
                        state = TickableStatus::EMPTY;
                        shape.name = NONE;
                    }
                    break;
                case DOWN:
                case LEFT:
                    if (CanSend(BuildingAllPos()[0], direction, Goods::RIGHT_CYCLE, gamemap) && CanSend(BuildingAllPos()[1], direction, Goods::LEFT_CYCLE, gamemap))
                    {
                        Send(BuildingAllPos()[0], direction, Goods::RIGHT_CYCLE, gamemap);
                        Send(BuildingAllPos()[1], direction, Goods::LEFT_CYCLE, gamemap);
                        state = TickableStatus::EMPTY;
                        shape.name = NONE;
                    }
                    break;
                default:
                    break;
            }
        default:
            break;
    }
    return;
}
